/**
 * Created By 李柏霖
 * 2025/3/23 20:37
 */

package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ArticleDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.sql.Wrapper;

@Service
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {
    @Override
    public ResponseResult selectList(WmNewsDto dto) {

        //参数校验
        //1.校验dto
        if (ObjectUtils.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //1.校验分页参数
        dto.checkParam();

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();

        //开始处理频道条件
        if (!StringUtils.isEmpty(dto.getChannelId())) {
            //channelId不为空
            wrapper.eq(WmNews::getChannelId, dto.getChannelId());
        }

        //开始处理关键字搜索
        if (!StringUtils.isEmpty(dto.getKeyword())) {
            //关键字不为空
            //模糊查询
            wrapper.like(WmNews::getTitle, dto.getKeyword());
        }

        //开始处理时间范围条件
        if (!StringUtils.isEmpty(dto.getBeginPubDate()) && !StringUtils.isEmpty(dto.getEndPubDate())) {
            //开始时间不为空
            wrapper.ge(WmNews::getPublishTime, dto.getBeginPubDate());
            wrapper.le(WmNews::getPublishTime, dto.getEndPubDate());
        }

        //开始处理状态条件
        if (!StringUtils.isEmpty(dto.getStatus())) {
            //状态不为空
            wrapper.eq(WmNews::getStatus, dto.getStatus());
        }

        Page<WmNews> wmNewsPage = new Page<>(dto.getPage(), dto.getSize());

        Page<WmNews> resultPage = page(wmNewsPage, wrapper);

        ResponseResult responseResult = new PageResponseResult((int) resultPage.getCurrent(), (int) resultPage.getSize(), (int) resultPage.getTotal());

        return responseResult.ok(resultPage.getRecords());
    }

    @Override
    public ResponseResult submit(ArticleDto dto) throws Exception {
        //参数判断
        if (ObjectUtils.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmNews wmNews = new WmNews();
        BeanUtils.copyProperties(dto, wmNews);
        if (!StringUtils.isEmpty(wmNews.getPublishTime())) {
            wmNews.setEnable(0);
        }
        save(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
